﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class BalanceCenter_AdminSendSMS : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!PublicFunction.CheckSystemRegisterState(this, "SMS_ON"))
        //{
        //    PublicFunction.GoError(ErrorNumber.Unknow, "对不起您的系统未激活，不能使用此功能!", this.GetType().BaseType.FullName);

        //    return;
        //}

        if (!this.IsPostBack)
        {
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/AdminSendSMS.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
    	DataTable dt = new DAL.Views.V_Customer().Open("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
        Panel1.Visible = (dt.Rows.Count > 0);
        Panel2.Visible = false;
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            //e.Item.Cells[10].Text = "";

            //********************************************************** 修改星星显示数目
            //int intCount = Shove._Convert.StrToInt(e.Item.Cells[14].Text, 1);  //2008.03.10添加，满足星星需求
            //for (int i = 0; i < intCount; i++)
            //{
            //    e.Item.Cells[10].Text += "<img src='../Images/gold.gif' border='0'>";
            //}
            //***********************************************************************************

            //for (int i = 0; i < Shove._Convert.StrToInt(e.Item.Cells[15].Text, 1); i++)
            //{
            //    e.Item.Cells[10].Text += "<img src='Images/gold.gif' border='0'>";
            //}

            CheckBox cb = (CheckBox)e.Item.Cells[11].FindControl("cbSurrogate");
            cb.Checked = Shove._Convert.StrToBool(e.Item.Cells[13].Text, true);
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void bt_AddSMS_ServerClick(object sender, EventArgs e)
    {
        System.Web.UI.WebControls.CheckBox chkBox;

        string SendUser = "";
        for (int i = 0; i < g.Items.Count; i++)
        {
            chkBox = (CheckBox)g.Items[i].FindControl("chkCompany");
            if (chkBox != null && chkBox.Checked)
            {
                if (SendUser != "")
                {
                    SendUser += ",";
                }

                SendUser += g.Items[i].Cells[9].Text;
            }
        }

        if (SendUser.ToString() != "")
        {
            tbSMSUser.Text = SendUser.ToString();

            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }

    protected void btnSendSMS_Click(object sender, EventArgs e)
    {
        if (tbSMSContent.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入手机短信息的内容！");

            return;
        }

        if (tbSMSUser.Text == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请先添加联系人信息！");

            return;
        }

        //Send SendMessage...
        int CountOK = 0, CountFail = 0;	//发送计数统计
        string SendResult = "";	//发送结果

        string[] ToSMSUser = tbSMSUser.Text.Split(',');
        for (int i = 0; i < ToSMSUser.Length; i++)
        {
            if (PublicFunction.SendSMS(_Site.ID, -1, ToSMSUser[i], tbSMSContent.Text.Trim()) < 0)
            {
                CountFail++;
                SendResult += "用户 " + ToSMSUser[i] + " 发送错误！<br />";

                continue;
            }

            CountOK++;
            SendResult += "用户 " + ToSMSUser[i] + " 发送成功。<br />";
        }

        labSendResult.Text = "发送结果：成功 " + CountOK.ToString() + " 个，失败 " + CountFail.ToString() + " 个。<br />" + SendResult;

        //Panel1.Visible = true;
        //Panel2.Visible = false;

        tbSMSContent.Text = "";

        //Shove._Web.JavaScript.Alert(this.Page, "手机短信发送成功！");

        return;
    }

    protected void bt_AllUserSMS_ServerClick(object sender, EventArgs e)
    {
        string SendUser = "";

        DataTable dt = new DAL.Tables.tbl_Customer().Open("Mobile", "", "");

        try
        {

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Mobile"].ToString() != "")
                {
                    if (SendUser != "")
                    {
                        SendUser += ",";
                    }

                    SendUser += dr["Mobile"].ToString();
                }
            }
        }
        catch
        {

        }

        if (SendUser.ToString() != "")
        {
            tbSMSUser.Text = SendUser.ToString();

            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }
}